package com.inforich.web.controller.building;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.building.domain.BuildingType;
import com.inforich.building.domain.RubbishType;
import com.inforich.building.service.IRubbishTypeService;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.common.utils.poi.ExcelUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 垃圾类型Controller
 *
 * @author hjm
 * @date 2022-10-29
 */
@RestController
@RequestMapping("/building/rubbishType")
public class RubbishTypeController extends BaseController {

  @Autowired
  private IRubbishTypeService rubbishTypeService;

  /**
   * 查询垃圾类型列表
   */
  @PreAuthorize("@ss.hasPermi('building:rubbishType:list')")
  @GetMapping("/list")
  public AjaxResult list(RubbishType rubbishType) {
    Map<String, Object> map = beanToMap(rubbishType);
    List<RubbishType> list = rubbishTypeService.listRubbishType(map);
    return AjaxResult.success(list);
  }


  /**
   * 导出垃圾类型列表
   */
  @PreAuthorize("@ss.hasPermi('building:rubbishType:export')")
  @Log(title = "垃圾类型", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, RubbishType rubbishType) {
    Map<String, Object> map = beanToMap(rubbishType);
    List<RubbishType> list = rubbishTypeService.listRubbishType(map);
    ExcelUtil<RubbishType> util = new ExcelUtil<RubbishType>(RubbishType.class);
    util.exportExcel(response, list, "垃圾类型数据");
  }

  /**
   * 获取垃圾类型详细信息
   */
  @PreAuthorize("@ss.hasPermi('building:rubbishType:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    return AjaxResult.success(rubbishTypeService.getById(id));
  }

  /**
   * 新增垃圾类型
   */
  @PreAuthorize("@ss.hasPermi('building:rubbishType:add')")
  @Log(title = "垃圾类型", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody RubbishType rubbishType) {
    return toAjax(rubbishTypeService.save(rubbishType));
  }

  /**
   * 修改垃圾类型
   */
  @PreAuthorize("@ss.hasPermi('building:rubbishType:edit')")
  @Log(title = "垃圾类型", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody RubbishType rubbishType) {
    return toAjax(rubbishTypeService.updateById(rubbishType));
  }

  /**
   * 删除垃圾类型
   */
  @PreAuthorize("@ss.hasPermi('building:rubbishType:remove')")
  @Log(title = "垃圾类型", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(rubbishTypeService.removeByIds(Arrays.asList(ids)));
  }

  /**
   * 查询垃圾类型列表（排除节点）
   */
  @PreAuthorize("@ss.hasPermi('building:rubbishType:list')")
  @GetMapping("/list/exclude/{id}")
  public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id) {
    List<RubbishType> depts = rubbishTypeService.listRubbishType(new HashMap<>());
    depts.removeIf(d -> d.getId().intValue() == id);
    return AjaxResult.success(depts);
  }

  /**
   * 树
   */
  @GetMapping("/tree")
  public AjaxResult selectBuildingTypeTreeList() {
    List<RubbishType> list = rubbishTypeService.list();
    return AjaxResult.success(rubbishTypeService.selectRubbishTypeTreeList(list));
  }
}
